package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryProjectReq;
import com.sg.dto.base.app.req.QueryProjectDetailReq;
import com.sg.dto.base.app.req.QueryProjectListReq;
import com.sg.entity.OmsProject;
import com.sg.service.base.app.MOmsProjectService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsProjectMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsProjectServiceImpl
        extends ServiceImpl<OmsProjectMapper, OmsProject>
        implements MOmsProjectService {

    @Autowired
    OmsProjectMapper omsProjectMapper;

    /**
     * 3-7-03新增协同任务[7740]
     * gen by moon at 11/8/2023, 3:43:30 PM
     */
    @Trace(operationName = "3-7-03新增协同任务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addProject(OmsProject omsProject) {
        omsProject.setProjectId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        // TODO: 2023/11/13 手写代码
        omsProject.setProjectCode("PR0" + CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsProject.setPercentageProgress(Double.valueOf("0"));//CUSTOM_CONVENTION
        omsProject.setTaskStatus("NOT_AT_THE");
        omsProject.setIsComplete("FALSE");
        omsProject.setIsOverdue("FALSE");
        omsProject.setOrderNumber(Long.valueOf(0));//CUSTOM_CONVENTION
        omsProject.setRelevantPeopleNum(Long.valueOf(0));//CUSTOM_CONVENTION
        omsProject.setIsArchive("FALSE");
        omsProject.setIsValid("FALSE");
        omsProject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsProject.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsProject != null && omsProject.getSubjectLifeCycle() != null && omsProject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsProject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsProject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsProject.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsProject != null && omsProject.getSubjectLifeCycle() != null && omsProject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsProject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsProject.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsProject.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsProject)) {
            return omsProject.getProjectId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-7-03修改协同任务[7741]
     * gen by moon at 11/8/2023, 3:43:31 PM
     */
    @Trace(operationName = "3-7-03修改协同任务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProject(OmsProject omsProject) {
        omsProject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsProject != null && omsProject.getSubjectLifeCycle() != null && omsProject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsProject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsProject != null && omsProject.getSubjectLifeCycle() != null && omsProject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsProject.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsProject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsProject != null && omsProject.getSubjectLifeCycle() != null && omsProject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsProject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsProject != null && omsProject.getSubjectLifeCycle() != null && omsProject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsProject.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsProject);
    }

    /**
     * 3-7-02查询协同任务列表[7749]
     * gen by moon at 12/7/2023, 6:14:55 PM
     */
    @Trace(operationName = "3-7-02查询协同任务列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsProject> queryProjectList(QueryProjectListReq reqDto) {

        try {
            QueryWrapper<OmsProject> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            // TODO: 2023/11/15 手写代码
            if (reqDto.getPlanStartTime() != null && reqDto.getPlanEndTime() != null) {
                queryWrapper.ge("plan_start_time", reqDto.getPlanStartTime());
                queryWrapper.le("plan_end_time", reqDto.getPlanEndTime());
                reqDto.setPlanStartTime(null);
                reqDto.setPlanEndTime(null);
            }

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsProject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsProject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsProject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsProject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-7-02批量查询协同任务[7763]
     * gen by moon at 12/16/2023, 8:27:46 PM
     */
    @Trace(operationName = "3-7-02批量查询协同任务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsProject> batchQueryProject(BatchQueryProjectReq reqDto) {

        try {
            QueryWrapper<OmsProject> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getProjectList() == null || reqDto.getProjectList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("project_id", reqDto.getProjectList());

            // TODO: 2023/11/15 手写代码
            if (reqDto.getPlanStartTime() != null && reqDto.getPlanEndTime() != null) {
                queryWrapper.le("plan_start_time", reqDto.getPlanStartTime());
                queryWrapper.ge("plan_end_time", reqDto.getPlanEndTime());
                reqDto.setPlanStartTime(null);
                reqDto.setPlanEndTime(null);
            }
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsProject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsProject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsProject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsProject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-7-03查询协同任务详情[7766]
     * gen by moon at 11/8/2023, 3:43:41 PM
     */
    @Trace(operationName = "3-7-03查询协同任务详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsProject queryProjectDetail(QueryProjectDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsProject> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsProject> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsProject retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-7-03删除协同任务(管理)[7769]
     * gen by moon at 11/8/2023, 3:43:42 PM
     */
    @Trace(operationName = "3-7-03删除协同任务(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteProject(String projectId) {

        return super.removeById(projectId);
    }
}
